﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.Maintainer
{
    [CPPEI.WebApi.AuthGetAction("Maintainer", "GetList", "1.0")]
    public class GetListAction : BaseAction<GetListParams, GetListResult>
    {
        protected override ActionResult<GetListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetListParams opParams)
        {
            ListResult<ENT.Maintainer> serResult = new SER.QueryService().QueryMantainerList(opParams.Filter);

            if(serResult.IsSuccess)
            {
                GetListResult result = new GetListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetListResult.Item item = new GetListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.Name = serResult.Data[index].Name;
                    item.Email = serResult.Data[index].Email;
                    item.Mobile = serResult.Data[index].Mobile;
                    item.StartTime = serResult.Data[index].StartTime;
                    item.EndTime = serResult.Data[index].EndTime;
                    result.Items.Add(item);
                }

                return new ActionResult<GetListResult>(result);
            }
            else
            {
                return new ActionResult<GetListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetListParams
    {
        [Parameter]
        public string Filter { get; set; }
    }

    public class GetListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public string Mobile { get; set; }
            public string StartTime { get; set; }
            public string EndTime { get; set; }
        }
    }
}
